package com.dk.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("file")
public class FileController {

    @RequestMapping("upLoadFile")
    @ResponseBody
    public Map<String, String> upLoadFile(@RequestParam(value = "myFile") MultipartFile myFile) {
        // 获取文件名
        String fileName = myFile.getOriginalFilename();
        fileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
        // 上传文件的路径，就是文件需要保存的文件夹地址
        String path = "E:\\java_4\\day21\\东东旅游网\\静态页面\\img\\product\\small\\";
        // 构建一个文件
        // 构建一个文件dowload
        File file = new File(path + fileName);
        // 调用上传方法，完成文件上传
        try {
            myFile.transferTo(file);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //{key:value}  @ResponseBody可以把实体类，list、Set，map、[{}] , {}
        Map<String, String> url = new HashMap<>();
        url.put("imgUrl", "img/product/small/" + fileName);
        return url;
    }

    @RequestMapping("download")
    public ResponseEntity<byte[]> download(String filename) {
        // 设置编码
        try {
            String newFileName = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
            // 文件所在路径
            String path = "E:\\java_4\\day21\\东东旅游网\\静态页面\\";
            // 文件下载的全路径
            String downPath = path + filename;// E:\\upload\\abc.jsp
            // 生成下载的文件
            File file = new File(downPath);
            // 设置HttpHeaders,使得浏览器响应下载
            HttpHeaders headers = new HttpHeaders();
            // 给浏览器设置下载文件名
            headers.setContentDispositionFormData("attachment", newFileName);
            // 浏览器响应方式
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            // 把文件以二进制格式响应给浏览器
            ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                    headers, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("dowload")
    public void dowload(String filename, HttpServletResponse resp) throws IOException {
        File file = new File("E:\\java_4\\day21\\东东旅游网\\静态页面\\" + filename);
        FileInputStream fis = new FileInputStream(file);
        resp.setHeader("Content-Disposition", "attachment;filename" + filename);
        ServletOutputStream os = resp.getOutputStream();
        byte[] arr = new byte[1024];
        int a;
        while ((a = fis.read(arr)) != -1) {
            os.write(arr);

        }
        os.close();
        fis.close();


    }
}
